// 个人中心
bangkeapp.controller('mycenterctrl', 
["$scope", "$state", "$stateParams", "userinfoService", "$ionicLoading", 
	"centerService", "fileurl", 
	function($scope, $state, $stateParams, userinfoService, $ionicLoading, 
		centerService, fileurl) {
		// 获取我的会员信息
		$ionicLoading.show({
			content: 'Loading',
			animation: 'fade-in',
			showBackdrop: true,
			maxWidth: 200,
			showDelay: 0
		});

		$scope.member = userinfoService.getUser();
       	$scope.fileurl = fileurl;
		$ionicLoading.hide();
		// 跳转到修改查看会员信息界面
		$scope.toViewMemberInfo = function() {
			$state.go('bangke.mymemberinfo');
		};

		// 获取股权积分
		centerService.interests().then(function(result) {
			$scope.interests = result.interests;
		}, function(error) {}, function(progress) {});
	}
]);
// 我的信息
bangkeapp.controller('mymemberinfoctrl', 
["$scope", "$state", "$stateParams", "centerService", "userinfoService", "$ionicModal",
	function($scope, $state, $stateParams, centerService, userinfoService, $ionicModal) {
		// 获取我的会员信息
		$scope.member = {};
		$scope.member = userinfoService.getUser();
		if (undefined != $scope.member.memberid && '' != $scope.member.memberid) {
			centerService.myinfo($scope.member).then(function(result) {
				var data = result;
				if(data.Response.Header.ResponseCode == "M") {
					$scope.member = data.Response.Body.Response[0];
				}
			}, function(error) {
				console.log("error!");
			}, function(progress) {
	
			});
			
			if($scope.member.memberlevel == "01" ||
				$scope.member.memberlevel == "" ||
				undefined == $scope.member.memberlevel) {
				$scope.member.memberleveldesc = "普通会员";
			} else if($scope.member.memberlevel == "02") {
				$scope.member.memberleveldesc = "高级会员";
			} else if($scope.member.memberlevel == "03") {
				$scope.member.memberleveldesc = "商户会员";
			}
			
			// 保存按钮是否显示:
			if (undefined == $scope.member.name || '' == $scope.member.name
				|| undefined == $scope.member.idcard || '' == $scope.member.idcard
				|| undefined == $scope.member.email || '' == $scope.member.email
				|| undefined == $scope.member.ylhaccount || '' == $scope.member.ylhaccount
				|| undefined == $scope.member.yhltel || '' == $scope.member.yhltel
				|| undefined == $scope.member.isidcardcheck || '0' != $scope.member.isidcardcheck) {
				$scope.savebtnshow = true;
			} else {
				$scope.savebtnshow = false;
			}
	
	        // 上传身份证照片模块
	        $ionicModal.fromTemplateUrl('uploadidcard.html', {
	            scope: $scope
	        }).then(function(modal) {
	            $scope.modal = modal;
	            var height = (window.screen.width / 2) * 0.6;
		        angular.element($scope.modal.$el[0].querySelectorAll('.div-upload')).css({"height" : height + "px"});
		        angular.element($scope.modal.$el[0].querySelectorAll('.upload-idcard-btn')).css({"height" : height + "px"});
	        });
	        
			// 上传
			$scope.toupload = function() {
				$scope.modal.show();
			}
			
			$scope.toReUpload = function(){
				$scope.modal.show();
			}
			
			$scope.upload = function(){
				event.target.parentElement.previousElementSibling.click();
			};
			
			$scope.previewimag = function() {
				
				var base64 = new UploadIdCardBase64(event.target);
				
				angular.element(event.target.nextElementSibling.children[0])
					.removeClass("img-idcard-upload").addClass("img-idcard-uploaded");
			}
		
			$scope.deleteimg = function() {
			    angular.element(event.target).parent().remove();
			    angular.element(document.querySelector('.img-idcard-uploaded'))
					.removeClass("img-idcard-uploaded").addClass("img-idcard-upload");
			}
			
			$scope.submitimg = function() {
				$scope.uploadidcardimg = [];
				$scope.uploadidcardimg[0] = angular.element(document.querySelectorAll('.img-item img')[0]).attr("src");
				$scope.uploadidcardimg[1] = angular.element(document.querySelectorAll('.img-item img')[1]).attr("src");
				$scope.uploadidcardimg[2] = angular.element(document.querySelectorAll('.img-item img')[2]).attr("src");
		    		
				$scope.modal.hide();
			}
			
			$scope.goback = function(){
				$scope.modal.hide();
			}
	
			$scope.savememberinfo = function() {
				
//				if ("2" != $scope.member.isidcardcheck) {
//					if (undefined == $scope.uploadidcardimg 
//							|| $scope.uploadidcardimg.length != 3) {
//						alert("请上传身份证照片!");
//						return false;
//					}
//					if ($scope.uploadidcardimg.length == 3) {
//						if (undefined == $scope.uploadidcardimg[0] || "" == $scope.uploadidcardimg[0] ) {
//							$scope.member.idcardPhotoBase64 = $scope.uploadidcardimg[0];
//						}
//						if (undefined == $scope.uploadidcardimg[1] || "" ==$scope.uploadidcardimg[1] ) {
//							$scope.member.idcardGovBase64 = $scope.uploadidcardimg[1];
//						}
//						if (undefined == $scope.uploadidcardimg[2] || "" == $scope.uploadidcardimg[2] ) {
//							$scope.member.idcardInHandBase64 = $scope.uploadidcardimg[2];
//						}
//						$scope.member.isidcardcheck = '1';
//					}
//				}
				
				centerService.savememberinfo($scope.member).then(function(result) {
					var data = result;
					if(data.Response.Header.ResponseCode == "M") {
						alert("保存成功!");
					} else {
						alert("保存失败! 原因: " + data.Response.Header.MsgList[0].MsgContent);
					}
				}, function(error) {
					console.log("error!");
				}, function(progress) {
	
				});
			}
		}
	}
]);
// 我的二维码
bangkeapp.controller('mycodectrl', 
["$scope", "$state", "$stateParams", "centerService", "userinfoService", "spreadurl", "fileurl",
	function($scope, $state, $stateParams, centerService, userinfoService, spreadurl, fileurl) {
		 
		$scope.userinfo = userinfoService.getUser();
		$scope.fileurl = fileurl;
		if (undefined != $scope.userinfo.memberid && '' != $scope.userinfo.memberid) {
			var base = new Base64();  
	        var str = base.encode("reference=" + $scope.userinfo.membername); 
			// 设置参数方式
			var qrcode = new QRCode('qrcode', {
				text: spreadurl + "?" + str,
				width: 200,
				height: 200,
				colorDark: '#f69c00',
				colorLight: '#ffffff',
				correctLevel: QRCode.CorrectLevel.H
			});
		}
	}
]);
// 我的初始股权
bangkeapp.controller('equityctrl', 
["$scope", "$state", "$stateParams", "centerService", "userinfoService", "spreadurl", "fileurl",
	function($scope, $state, $stateParams, centerService, userinfoService, spreadurl, fileurl) {
	    //获取股权积分
	    $scope.searchEquity=function(){
	    		centerService.equity().then(function(data){
	    			
	    		})
	    };
		var init=function(){
			 $scope.items = [
	    {
			time:"2018-01-10",
			income:"收入",
			money:"1.2",
			member:"帮客"
		},
	    {
			time:"2018-01-10",
			income:"收入",
			money:"1.2",
			member:"帮客"
		}
	  ];
		};
		init();
	}
]);
// 上传身份证照片
bangkeapp.controller('uploadidcardctrl', ["$scope", "$state", "$stateParams", "userinfoService", "fileReader", "url", "$http",
	function($scope, $state, $stateParams, userinfoService, fileReader, url, $http) {
//		$scope.userinfo = userinfoService.getUser();
//		$scope.data = {
//			file: null,
//			defaultImage: 'img/yunlianhuiicon.png'
//		};
//
//		$scope.$watch('data.file', function(newValue, oldValue, scope) {
//			if($scope.data.file) {
//				$scope.upload($scope.data.file);
//			}
//		});
//
//		// 上传图片  
//		$scope.upload = function(file) {
//			Upload.upload({
//				// 服务端接收  
//				url: 'upload/url',
//				// 上传的同时带的文件和参数  
//				data: {
//					file: file,
//					'username': $scope.username
//				}
//			}).then(function(resp) {
//				console.log('Success ' + resp.config.data.file.name + 'uploaded. Response: ' + resp.data);
//			}, function(resp) {
//				console.log('Error status: ' + resp.status);
//			}, function(evt) {
//				var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
//				console.log('progress: ' + progressPercentage + '% ' + evt.config.data.file.name);
//			});
//		};

	}
]);
